// 手册加载
window.onload = function () {
    var url = 'http://192.168.0.108:8080/ManualManage/getAllManualByPage'
    function s(datas) {
        function operateFormatter(value, row, index) {//赋予的参数
            return [
                '<button class="btn btn-primary" style="margin-right:10px"  onclick="edithandbook(' + row.manualid + ')">编辑</button>'
            ].join('');
        }
        // 设置table列样式
        function formatFormatter(value, row, index) {
            if (value == 1) {

                return [

                    '<img src="../../../images/success.png" onclick="statusChange(' + row.validflag + ',' + row.manualid + ')">',
                ].join('');
            } else {
                return [

                    '<img src="../../../images/fail.png" onclick="statusChange(' + row.validflag + ',' + row.manualid + ')">',
                ].join('');
            }
        }
        var tableColumns = [
            {
                field: 'manualid',
                title: 'ID',
                switchable: true
            }, {
                field: 'title',
                title: '标题',
                switchable: true
            },
            {
                field: 'validflag',
                title: '状态',
                formatter: formatFormatter
            }, {
                field: 'operate',
                title: '操作',
                formatter: operateFormatter //自定义方法，添加操作按钮
            }
        ];
        $("#handbooktable").bootstrapTable('destroy'); //分页必须写
        $("#handbooktable").bootstrapTable({
            cache: false,
            pagination: true,
            sidePagination: "client",  //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,   //初始化加载第一页，默认第一页
            pageSize: 5,   //每页的记录行数（*）
            pageList: [5, 10], //可供选择的每页的行数
            columns: tableColumns,
            data: datas,
            queryParamsType: '',
            //得到查询的参数
            queryParams: function queryParams(params) {
                return params;
            },
            onLoadSuccess: function (data) {  //加载成功时执行
                alert("成功" + data)
            },
            onLoadError: function () {  //加载失败时执行
                alert("失败")
            }

        });
    }
    $.ajax({
        url: url,
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        success: function (res) {
            console.log(res.data)
            s(res.data)

        }
    })
}
function addhandbook(e) {
    // 添加
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "none";
    };
    divsetadd = document.getElementsByClassName("addhandbook");
    for (var i = 0; i < divset.length; i++) {
        divsetadd[i].style.display = "inline";
    };
}
// 添加保存手册
function addsavehandbook() {
    //获取值
    var addtitle = document.getElementById("addtitle").value
    var addcontent = $("#addcontent").val()
    console.log(addtitle)
    var date = {
        title: addtitle,
        content: addcontent,
        contentTxt: ""
    }
    var datas = JSON.stringify(date)
    $.ajax({
        url: 'http://192.168.0.108:8080/ManualManage/addManual',
        method: 'post',
        contentType: "application/json",
        data: datas,
        success: function (res) {
            console.log(res.data)
            console.log("已成功")
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "inline";
                location.reload()
            };
            divsetedit = document.getElementsByClassName("addhandbook");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "none";
            };
            divsetadd = document.getElementsByClassName("edithandbook");
            for (var i = 0; i < divset.length; i++) {
                divsetadd[i].style.display = "none";
            };
        }
    })
   
}
function backhandbook(e) {
    // 返回
    divset = document.getElementsByClassName("search");
    for (var i = 0; i < divset.length; i++) {
        divset[i].style.display = "inline";
    };
    divsetedit = document.getElementsByClassName("addhandbook");
    for (var i = 0; i < divset.length; i++) {
        divsetedit[i].style.display = "none";
    };
    divsetadd = document.getElementsByClassName("edithandbook");
    for (var i = 0; i < divset.length; i++) {
        divsetadd[i].style.display = "none";
    };
}
// 手册编辑按钮
function edithandbook(manualid) {
    console.log(manualid + "id")
    // 获取
    $.ajax({
        url: 'http://192.168.0.108:8080/ManualManage/getManualByID',
        method: 'post',//请求方式
        contentType: "application/x-www-form-urlencoded",
        data: {
            manualID: manualid
        },
        success: function (res) {
            console.log(res)
            console.log(res.data)
            // 
            $("#edittitle").val(res.data.title)
            $("#editcontent").val(res.data.content)
            $("#manuaid").val(res.data.manualid)
            // 编辑
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "none";
            };
            divsetedit = document.getElementsByClassName("edithandbook");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "inline";
            };
        }
    })

}
// 编辑手册保存
function editsaveCity() {
    // 获取值
    var edittitle = document.getElementById("edittitle").value
    var editcontent = document.getElementById("editcontent").value
    var editmanuaid = document.getElementById("manuaid").value
    console.log("name" + edittitle + "abab" + editmanuaid)
    var date = {
        content: editcontent,
        contentTxt: "",
        manualID: editmanuaid,
        title: edittitle
    }
    var datas = JSON.stringify(date)
    // 后台请求
    $.ajax({
        url: 'http://192.168.0.108:8080/ManualManage/updateManual',//请求后台的url
        method: 'post',//请求方式
        contentType: "application/json",
        data: datas,
        success: function (res) {
            console.log(res.msg)
            //返回
            divset = document.getElementsByClassName("search");
            for (var i = 0; i < divset.length; i++) {
                divset[i].style.display = "inline";
                location.reload()
            };
            divsetedit = document.getElementsByClassName("addhandbook");
            for (var i = 0; i < divset.length; i++) {
                divsetedit[i].style.display = "none";
            };
            divsetadd = document.getElementsByClassName("edithandbook");
            for (var i = 0; i < divset.length; i++) {
                divsetadd[i].style.display = "none";
            };
        }
    })

}
// 状态变化
// 当前版本手册状态
function statusChange(validflag,manualid) {
    // 获取当前状态
    console.log(validflag);
    // 判断当前状态更改
    if(validflag === 1){
       $('#modal-input').html('确认停用');
       $('#myModal').modal('show');
        var manualstatus=0
        console.log("dianji"+manualstatus+manualid);
        changeStata(manualstatus,manualid)
    }
    else{
       $('#modal-input').html('确认启用');
       $('#myModal').modal('show');
        var manualstatus=1
        console.log("dianji"+manualstatus+manualid);
        changeStata(manualstatus,manualid)
    }
}
// 城市状态确认更改按钮
// 切换状态
function changeStata(manualstatus,manualid) {
    $('#btn_ok').click(function(){
    $.ajax({
        url: 'http://192.168.0.108:8080/ManualManage/updateManualStatus',
        method:'post',
        contentType:'application/x-www-form-urlencoded',
        data:{
            manualID:manualid,
            validFlag:manualstatus
        },
         success: function (res) {
            console.log(res.msg);
            $('#myModal').modal('hide');
            location.reload();
         }
    })
})
}